package options

import (
	"fmt"

	"github.com/spf13/pflag"
	"github.com/spf13/viper"
)

// ApplicationOption 应用相关的配置
type ApplicationOption struct {
	CmsAppId string `mapstructure:"cms-app-id" json:"cms-app-id"`
	SubName  string `mapstructure:"sub-name"   json:"sub-name"` // 子系统名称
	AppName  string `mapstructure:"app-name"   json:"app-name"` // 应用名称
}

func NewApplicationOption() *ApplicationOption {
	return &ApplicationOption{
		AppName:  "",
		CmsAppId: "",
	}
}

func (o *ApplicationOption) AddFlags(fs *pflag.FlagSet) {
	fs.StringVar(&o.CmsAppId, "application.cms-app-id", o.CmsAppId, "Specify application cmsAppId from orchid.pab.com.cn.")
	fs.StringVar(&o.SubName, "application.sub-name", o.SubName, "Specify application sub system name from orchid.pab.com.cn.")
	fs.StringVar(&o.AppName, "application.app-name", o.AppName, "Specify application name from orchid.pab.com.cn.")
}

func (o *ApplicationOption) Validate() []error {
	var errs = []error{}
	if len(o.CmsAppId)*len(o.SubName)*len(o.AppName) == 0 {
		errs = append(errs, fmt.Errorf("--application.sub-name, --application.cms-app-id or --application.app-name can't empty"))
	}
	return errs
}

func GetAppName() string {
	return viper.GetString("application.app-name")
}

func GetAppId() string {
	return viper.GetString("application.cms-app-id")
}

func GetSubName() string {
	return viper.GetString("application.sub-name")
}

func GetServiceName() string {
	return GetSubName() + "-" + GetAppId() + "-" + GetAppName()
}
